import 'package:flutter/cupertino.dart';
import 'package:hitutor/UI/AudioPlayerPage.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/HttpGo.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/ChooserBox.dart';
import 'package:hitutor/Widget/MyBgPage.dart';
import 'package:hitutor/Widget/MyButton.dart';
import 'package:hitutor/Widget/MyDialog.dart';
import 'package:hitutor/Widget/MyImage.dart';

import '../../VideoPlayerPage.dart';

class TeachMaterialList extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _TeachMaterialList();
  }
}

class _TeachMaterialList extends State<TeachMaterialList> {
  dynamic lesson = {};
  List<dynamic> listLesson = [];

  ChooserClass() {
    showWheelDialog2(context, "", listLesson, "displayLessonName", (index) {
      lesson = listLesson[index];
      chapter = {};
      unit = "";
      getList2();
      listUnit.clear();
      setState(() {});
    });
  }

  List<dynamic> listChapter = [];
  dynamic chapter = {};

  ChooserChapter() {
    showWheelDialog2(context, "", listChapter, "materialName", (index) {
      chapter = listChapter[index];
      unit = "";
      getList3();
      setState(() {});
    });
  }

  List<String> listUnit = [];
  String unit = "";

  ChooserUnit() {
    showWheelDialog(context, "", listUnit, (index) {
      unit = listUnit[index];
      setState(() {});
    });
  }

  int materialNum = 0;

  @override
  void initState() {
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      getList1();
    });
    super.initState();
  }

  getList1() {
    HttpGo(context).get("/Material/GetMaterialLessonOptions?studentId=${Global.studentId}", {}, (data) {
      listLesson = data["materialLessonOptions"];
    }, (code, error) => showToast(error));
  }

  getList2() {
    if (lesson == null) return;
    HttpGo(context).get("/Material/GetMaterialCodeOptions?lessonId=${lesson["lessonId"]}", {}, (data) {
      listChapter = data["materialCodeOptions"];
    }, (code, error) => showToast(error));
  }

  getList3() {
    listUnit.clear();
    HttpGo(context).get("/Material/GetMaterialUnitOptions?materialCode=${chapter["materialCode"]}", {}, (data) {
      for (var item in data["materialUnitOptions"]) {
        listUnit.add(item.toString());
      }
    }, (code, error) => showToast(error));
  }

  getFiles() {
    if (lesson == null || chapter == null || unit.isEmpty) return;
    HttpGo(context).post("/Material/GetMaterialPage", {
      "lessonId": lesson["lessonId"],
      "materialCode": chapter["materialCode"],
      "materialUnit": unit,
    }, (data) {
      PageUtil().open(context, AudioPlayerPage(Files: data["materials"]));
    }, (code, error) => showToast(error));
  }

  @override
  Widget build(BuildContext context) {
    return MyBgPage(
        "教材專區",
        Container(
          child: Column(children: [
            ChooserBox(lesson["displayLessonName"] ?? "",
                hintText: "選擇課程", bgColor: MyColor.White, onTap: ChooserClass, margin: EdgeInsets.fromLTRB(16, 16, 16, 4)),
            ChooserBox(chapter["materialName"] ?? "",
                hintText: "選擇教材", bgColor: MyColor.White, onTap: ChooserChapter, margin: EdgeInsets.fromLTRB(16, 4, 16, 4)),
            ChooserBox(unit, hintText: "選擇單元", bgColor: MyColor.White, onTap: ChooserUnit, margin: EdgeInsets.fromLTRB(16, 4, 16, 8)),
            Button("GO", height: 32, fontSize: 14, margin: EdgeInsets.only(top: 32), onTap: () {
              getFiles();
            }),
            // Expanded(
            //     child: ListView.builder(
            //         itemCount: materialNum,
            //         itemBuilder: (BuildContext context, int index) {
            //           return ItemMaterial(index == 1, index == 1 ? "教材-視頻" : "教材-音頻", onTap: () {
            //             index == 1
            //                 ? PageUtil().open(context, VideoPlayerPage(Global.videoUrl))
            //                 : PageUtil().open(context, AudioPlayerPage());
            //           });
            //         }))
          ]),
        ));
  }
}

class ItemMaterial extends StatelessWidget {
  String materialName;
  Function()? onTap;
  bool? allowDownload = false;
  bool isvideo;

  ItemMaterial(this.isvideo, this.materialName, {this.onTap, this.allowDownload});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
            padding: EdgeInsets.all(8),
            height: 56,
            margin: EdgeInsets.fromLTRB(16, 4, 16, 4),
            decoration: Global.getWhiteBg_shadow(4, MyColor.EE),
            child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
              LocalImage(isvideo ? "file_video.png" : "file_audio.png", width: 24, padding: EdgeInsets.only(right: 10)),
              Expanded(child: Text("$materialName", style: TextStyle(fontSize: 14, color: MyColor.Green))),
              (allowDownload ?? false) ? LocalImage("direct-download.png", width: 20, padding: EdgeInsets.all(10)) : Container(),
            ])));
  }
}
